package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理", tags = "channel", description = "频道管理API")
public class AdChannelController {

    @Autowired
    private AdChannelService channelService;

    @ApiOperation(value = "根据条件分页查询频道信息", notes = "按照name模糊分页查询，结果按照ord升序排序")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto) {
        return channelService.findByNameAndPage(dto);
    }

    @ApiOperation("保存频道信息")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel adChannel) {
        return channelService.insert(adChannel);
    }

    @ApiOperation("修改频道信息")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel){
        return channelService.update(adChannel);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/del/{id}")

    public ResponseResult delete(@PathVariable("id") Integer id){
//        System.out.println(1 / 0);
//        if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        return channelService.deleteById(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }

}

